/**
 * 工具箱
 */

let utils = {
  routerUrl:['#/headers','#/main','#/skins'],
  component_property_map: [
    ['v-address', 'pro-input'],
    ['v-checkbox', 'pro-checkbox'],
    ['v-date', 'pro-date'],
    ['v-time', 'pro-date'],
    ['v-datetime', 'pro-date'],
    ['v-email', 'pro-input'],
    ['v-hr', 'pro-hr'],
    ['v-hr-dashed', 'pro-hr'],
    ['v-hr-dotted', 'pro-hr'],
    ['v-hr-double', 'pro-hr'],
    ['v-input', 'pro-input'],
    ['v-input-currency', 'pro-currency'],
    ['v-input-number', 'pro-number'],
    //['v-page', 'pro-page'],
    ['v-radio', 'pro-checkbox'],
    ['v-select-multi', 'pro-multi-select'],
    ['v-select', 'pro-select'],
    ['v-table-default', 'pro-table'],
    ['v-table-left', 'pro-table-left'],
    ['v-table-top', 'pro-table-top'],
    ['v-tel', 'pro-input'],
    ['v-textarea', 'pro-input'],
    ['v-upatta', 'pro-upatta'],
    ['v-upimg', 'pro-upimg'],
    ['v-word', 'pro-word'],
  ],
  toStep:function(step){
    window.location.href=this.routerUrl[step]
  },
  /**
   * 获取默认的属性配置
   * 根据不同的组件构造不同的属性对象
   * @param {Object} component 组件名称 如 v-input
   */
  get_default_property:function(component){
    
    var default_props={
      //公共属性
      title:'默认标题',
      intro:'文字描述',
      placeholder:'必填',//提示
      required:true, //必填
      layout:'LR',//布局 UD 上下 LR 左右
      titleAlign:'left',
      introAlign:'left',
      titleWidth:2,//标题宽度
      selfWidth:100,//自身宽度
      columnCount:4,//列数
      
      //常用属性
      options:[],
      limitLen:false,
      minLen:0,//最小长度
      maxLen:0//最大长度
      
    }
    
    // 自身属性
    
    switch(component)
    {
      case "v-hr":
        default_props.hr={
          lineStyle:'hr',
          fillWidth:true
        }
        break;
      case "v-word":
        default_props.word={
          text:'',
          simple:'',
          limitLength:100,
          fold:true,
        }
        break;
      case "v-page":
        default_props.page={
          upText:'上一页',
          downText:'下一页'
        }
        break;
      case "v-select-multi":
        default_props.multiSelect={
          levels:['一级'],
          path:'',
          options:[]
        }
        break;
      case "v-checkbox":
        default_props.otherOption={
          selected:false,
          show:false,
          title:'其他',
          required:false
        }
        break;
    }
    
    if(component =="v-date" || component == "v-datetime" || component == "v-time"){
      default_props.date={
        defaultCurrentDate:true,
        isLimitDate:false,
        isLimitWeek:false,
        isLimitTime:false,
        dataType:'日期',
        dateFormat:'yyyy-MM-dd',
        timeFormat:'HH:mm:ss',
        timeFormat:'',
        limitDate:[],
        limitWeek:[],
        limitTime:[]
      }
      
      default_props.time = {
        selectableRange:''
      }
    }
    
    if(component == "v-table-default" || component == "v-table-top" || component == "v-table-left"){
      default_props.table={
        type:'radio',
        title:"总标题",
        titleWidth:20,
        rowCount:3,
        colCount:3,
        rows:[],
        cols:[]
      }
    }
    
    if(component == "v-upatta" || component == "v-upimg"){
      default_props.upfile={
        extend:{
          intro:'点击选择文件，文件不大于 xx MB'
        },
        isLimitFileType:false,
        isLimitFileSize:false,
        isLimitImgSize:false,
        multiFile:false,
        fileType:[],
        fileSize:0,
        fileCount:1,
        minWidth:0,
        maxWidth:0,
        minHeight:0,
        maxHeight:0
      }
    }
    
    if(component == "v-input-number" || component == "v-input-currency"){
      default_props.numberOption={
        unit:'¥',
        limitValue:false,
        min:0,
        max:0,
        limitDecimal:false,
        decimal:0,
        allowNegative:false
      }
    }
    
    return default_props;
  },
  get_component_property: function (componentType) {
    var proType = ''
    this.component_property_map.forEach(function (element, index, array) {
      if (element[0] === componentType) {
        proType = element[1]
      }
    })
    return proType
  },
  indexOf:function(items,id){
    for (var i = 0; i < items.length; i++) {
      if (items[i].id === id){
        return i
      }
    }
    return -1
  },
  get: function (items, id) {
    for (var i = 0; i < items.length; i++) {
      if (items[i].id === id) return items[i]
    }
  },
  indexOf: function (items, id) {
    for (var i = 0; i < items.length; i++) {
      if (items[i].id === id) return i
    }
    return -1
  },
  remove: function (items, id) {
    var index = this.indexOf(items, id)
    if (index > -1) {
      items.splice(index, 1)
    }
  },
  sort: function (items) {
    var compare = function (obj1, obj2) {
      if(obj1.id=='final-place'){
        return 1
      }
      var val1 = obj1.position
      var val2 = obj2.position
      if (val1 < val2) {
        return -1
      } else if (val1 > val2) {
        return 1
      } else {
        return 0
      }
    }
    items.sort(compare)
    // reset position
    for (var i = 0; i < items.length; i++) {
      items[i].position = i
    }
  },
  cloneArray:function(src){
    var list =[]
    for(var i=0;i<src.length;i++){
      list.push(this.shallowCopy(src[i]))
    }
    return list
  },
  shallowCopy: function (src) {
    var dst = {}
    for (var prop in src) {
      if (src.hasOwnProperty(prop)) {
        dst[prop] = src[prop]
      }
    }
    return dst
  },
  cloneObj:function(obj){
    var newObj = {};  
    if (obj instanceof Array) {  
        newObj = [];  
    }  
    for (var key in obj) {  
        var val = obj[key];  
        //newObj[key] = typeof val === 'object' ? arguments.callee(val) : val; //arguments.callee 在哪一个函数中运行，它就代表哪个函数, 一般用在匿名函数中。  
        newObj[key] = typeof val === 'object' ? this.cloneObj(val): val;  
    }  
    return newObj;  
  },
  /**
   * 移动组件位置
   * @param {Object} items
   * @param {Object} id
   * @param {Object} position
   */
  item_move:function(items,id,position){
    var item = this.get(items,id)
    item.position = position
    this.sort(items)
  },
  /**
   * 删除指定ID
   * @param {Object} items
   * @param {Object} id
   */
  item_remove: function (items, id) {
    var row = this.get(items,id).row
    this.remove(items, id)
    this.rowRearrange(items,row)
    this.sort(items)
    this.sumRowNumber(items)
  },
  /**
   * 重新排列行，自适应行宽度
   * @param {Object} items
   */
  rowRearrange:function(items,row){
    var rowItems = this.findComponentByRow(items,row)
    var count = rowItems.length
    if(count>0){
      var avgWidth = parseInt(100/count)
      for(var i=0;i<rowItems.length;i++){
        rowItems[i].property.selfWidth = avgWidth
      }
    }
  },
  /**
   * 上挤， 与上方的组件合并在同一行
   * 
   *  //找上一个组件，判断其宽度
      //如果为 100 ，则设置两个组件的宽度为50/50
      //如果不为100 ， 则一直网上找，知道所有组件的宽度小于等于100
      //将其100/所有组件数得到各个组件的宽度
      
   * @param {Object} items
   * @param {Object} id
   */
  item_up: function (items, id) {
    var selfItem = this.get(items, id)
    //判断上面行的数量
    var rowItmes = this.findComponentByRow(items,selfItem.row -1)
    if(rowItmes.length<5){
      selfItem.row = selfItem.row-1
      this.rowRearrange(items,selfItem.row)
      this.sumRowNumber(items)
    }else{
      alert('超出行承载范围，1行最多支持5个组件')
    }
  },
  /**
   * 下移实际为将其自身宽度撑开100%占据一行
   * 将原在行的数据平分宽度
   * @param {Object} items
   * @param {Object} id
   */
  item_down: function (items, id) {
    var selfItem = this.get(items, id)
    selfItem.property.selfWidth=100
    var row = selfItem.row
    var rowItems = this.findComponentByRow(items,selfItem.row)
    //取这一行的最后一个
    if(rowItems.length>0){ 
      var lastItem = rowItems[rowItems.length-1]
      var lastIndex = this.indexOf(items,lastItem.id)
      this.remove(items, id) //先移除
      this.item_insert(items,selfItem,lastIndex)
    }
    this.sumRowNumber(items)
    this.rowRearrange(items,row)
    this.sumRowNumber(items)
  },
  item_copy: function (items, id) {
    var srcItem = this.get(items, id)
    var newItem = {
      component: srcItem.component,
      id: srcItem.component + new Date().getTime(),
      position: srcItem.position+1,
      property:this.get_default_property(srcItem.component),
      handle:{
        show:false,
        up:false,
        down:false
      },
      value:'',
      values:[]
    }
    //copy item property  复制公有的属性
    newItem.property.title = srcItem.property.title;
    newItem.property.intro = srcItem.property.intro;
    newItem.property.placeholder = srcItem.property.placeholder;
    newItem.property.required = srcItem.property.required;
    newItem.property.layout = srcItem.property.layout;
    newItem.property.titleWidth = srcItem.property.titleWidth;
    newItem.property.selfWidth = srcItem.property.selfWidth;
    
    items.push(newItem)
    this.sort(items)
  },
  item_insert:function(items,item,index){
    item.position=index
    items.splice(index,0,item)
    this.item_selected(items,item.id,true)
  },
  item_selected: function (items,id, selected) {
    for (var i = 0; i < items.length; i++) {
      items[i].handle.show = false
      if (items[i].id === id) {
        items[i].handle.show = selected
      }
    }
  },
  /**
   * 计算所在行号
   * 
   * @param {Object} items
   */
  sumRowNumber:function(items){
    var compWidth = 0,row = 0 
    for(var i=0;i<items.length;i++){
      var item =  items[i]
      item.row = row
      compWidth += item.property.selfWidth
      if(compWidth > 100 || item.property.selfWidth == 100){
        row++
        compWidth=0
        item.row = row
      }
    }
  },
  /**
   * 查询该行的所有组件
   * @param {Object} items
   * @param {Object} row
   */
  findComponentByRow:function(items,row){
    var result = []
    for(var i=0;i<items.length;i++){
      var item =  items[i]
      if(item.row == row){
        result.push(item)
      }
    }
    return result
  }
  
}
export default utils
